package com.iengoo.servlet.ctrl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2019-01-28 16:06
 */
@WebServlet(urlPatterns = "/*")
public class StationCtrl extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        long l = System.currentTimeMillis();

//        // 获取前端带上来的cookie
        Cookie[] cookies = req.getCookies();

        for (int i = 0; i < cookies.length; i++) {
            System.out.println(cookies[i]);
        }


//        创建一个cookie对象
        Cookie cookie = new Cookie("username", "张三");
        Cookie cookie2 = new Cookie(URLEncoder.encode("密码"), "123456");

        // 将cookie放到响应对象中
        resp.addCookie(cookie);
        resp.addCookie(cookie2);


        HttpSession session = req.getSession();
        session.setAttribute("loginUsername", "张三" + l);

        System.out.println(session);
    }
}
